package com.sky.context;

import com.sky.constant.JwtClaimsConstant;
import com.sky.properties.JwtProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 后台当前上下文
 */
public class BaseContext {

    /*
    public static void removeCurrentId() {
        threadLocal.remove();
    } */

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();


    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    /**
     * 获取当前登录用户的ID
     */
    public static Long getEmpID() {
        Object value = get(JwtClaimsConstant.EMP_ID);
        return Long.parseLong(value.toString());
    }

    /**
     * 设置当前登录用户的userid
     */
    public static void setEmpID(Long userId) {
        set(JwtClaimsConstant.EMP_ID, userId);
    }

    /**
     * 获取当前登录用户的ID
     */
    public static Long getUserID() {
        Object value = get(JwtClaimsConstant.USER_ID);
        return Long.parseLong(value.toString());
    }

    /**
     * 设置当前登录用户的userid
     */
    public static void setUserID(Long userId) {
        set(JwtClaimsConstant.USER_ID, userId);
    }

    /**
     *  获取当前登录用户的code
     * @return
     */

    public static String getUserCode() {
        Object value = get(JwtClaimsConstant.USERNAME);
        return getObjectValue(value);
    }

    /**
     * 设置当前登录用户的userCode
     * @param userCode
     */

    public static void setUserCode(String userCode) {
        set(JwtClaimsConstant.USERNAME, userCode);
    }

    /**
     * 获取当前登录用户的名称
     */
    public static String getUserName() {
        Object value = get(JwtClaimsConstant.NAME);
        return getObjectValue(value);
    }

    /**
     *  设置当前登录用户的用户名称
     */
    public static void setUserName(String userName) {
        set(JwtClaimsConstant.NAME, userName);
    }

    /**
     * 获取本线程ThreadLocal存储的所有信息
     */
    public static Map<String, Object> getAll() {
        return threadLocal.get();
    }

    /**
     * 清空本线程ThreadLocal存储的信息
     */
    public static void remove() {
        threadLocal.remove();
    }

    /**
     * 获取当前登录用户的ID
     */
    public static Long getCurrentId() {
        Object value = get(JwtClaimsConstant.USER_ID);
        return Long.parseLong(value.toString());
    }

    /**
     * 设置当前登录用户的userid
     */
    public static void setCurrentId(Long userId) {
        set(JwtClaimsConstant.USER_ID, userId);
    }

    /**
     * 删除本线程ThreadLocal存储的指定KEY
     */
    public static void removeKey(String key) {
        Map<String, Object> threadLocalMap = threadLocal.get();
        Iterator<String> iterator = threadLocalMap.keySet().iterator();
        while (iterator.hasNext()) {
            String k = iterator.next();
            if (k.equals(key)) {
                iterator.remove();
            }
        }
    }

    private static String getObjectValue(Object obj) {
        return obj == null ? "" : obj.toString();
    }

}

